#include "StdInc.h"

#include "rwdriver.d3d12.hxx"

#include "pluginutil.hxx"

namespace rw
{

#ifndef _COMPILE_FOR_LEGACY

d3d12DriverInterface::d3d12NativeDriver::d3d12NativeDriver( d3d12DriverInterface *env, Interface *intf )
{
    EngineInterface *engineInterface = (EngineInterface*)intf;
    
    this->engineInterface = engineInterface;
    
    // Initialize the Direct3D 12 device.
    HRESULT deviceSuccess = 
        env->D3D12CreateDevice(
            NULL,
            D3D_FEATURE_LEVEL_11_0,
            IID_PPV_ARGS( &this->m_device )
        );

    if ( !SUCCEEDED(deviceSuccess) )
    {
        throw RwException( "failed to allocate Direct3D 12 device handle" );
    }

    // Cache some things.
    this->m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_RTV );

    // Success!
}

d3d12DriverInterface::d3d12NativeDriver::d3d12NativeDriver( const d3d12NativeDriver& right )
{
    this->engineInterface = right.engineInterface;

    throw RwException( "cannot clone a Direct3D 12 driver" );
}

d3d12DriverInterface::d3d12NativeDriver::~d3d12NativeDriver( void )
{
    // We automatically release main device handles.
    // TODO: release advanced device resources (rasters, geometries, etc)
}

d3d12DriverInterface::d3d12DriverFactory_t d3d12DriverInterface::d3d12DriverFactory;

static PluginDependantStructRegister <d3d12DriverInterface, RwInterfaceFactory_t> d3d12DriverReg;

#endif //_COMPILE_FOR_LEGACY

void registerD3D12DriverImplementation( void )
{
#ifndef _COMPILE_FOR_LEGACY
    // Put the driver into the ecosystem.
    d3d12DriverReg.RegisterPlugin( engineFactory );

    // TODO: register all driver plugins.
#endif //_COMPILE_FOR_LEGACY
}

};